const PAGE_ITEMS = 30;
var ITEM_HEIGHT = 0;
var VIEW_ITEMS = 0;
var TOTAL_ITEMS = 0;

cc.Class({
    extends: cc.Component,

    properties: {
        itemNode: cc.Node,
        txtBet: cc.Label,
        txtPos: cc.Label,
    },

    onLoad () {
        this.currPage = 1;
        this.allItems = [];
        this.itemData = null;
        this.isDataEnd = false;
        this.isSyncing = true;
        this.lastOffset = 0;
        this.container = this.itemNode.parent;
        this.itemNode.parent = null;
        this.scroller = this.node.getComponent(cc.ScrollView);
        ITEM_HEIGHT = this.itemNode.height;
        VIEW_ITEMS = Math.ceil(this.scroller.node.height / this.itemNode.height) + 1;
        
        cc.Linker(URLS.AwardRankToday, {
            page: 1,
            pageSize: 99,
            value: LINK.gameType
        }).request(data => {
            this.txtBet.string = data.nowAmount;
            this.txtPos.string = data.nowRank == 0 ? '未上榜' : data.nowRank;
            this.nowRank = data.nowRank;
            TOTAL_ITEMS = data.rankList.length;
            if(TOTAL_ITEMS > VIEW_ITEMS) {
                this.initView(data.rankList, this.nowRank);
            } else {
                for(var i = 0; i < TOTAL_ITEMS; i++) {
                    var obj = cc.instantiate(this.itemNode);
                    obj.parent = this.container;
                    obj.y = -ITEM_HEIGHT / 2 - ITEM_HEIGHT * i;
                    var item = this.initItem(obj, data.rankList[i], i);
                    item.order = i;
                    this.allItems.push(item);
                }
                this.container.height = TOTAL_ITEMS * ITEM_HEIGHT;
            }
        });
    },

    initView(data, rank) {
        var views = Math.floor(this.node.height / ITEM_HEIGHT);
        var start = rank;
        if(rank == 0 || rank > TOTAL_ITEMS - VIEW_ITEMS) start = TOTAL_ITEMS - VIEW_ITEMS;
        else if(rank <= views) start = 0;
        for(var i = start; i < start + VIEW_ITEMS; i++) {
            var obj = cc.instantiate(this.itemNode);
            obj.parent = this.container;
            obj.y = -ITEM_HEIGHT / 2 - ITEM_HEIGHT * i;
            var item = this.initItem(obj, data[i], i);
            item.order = i;
            this.allItems.push(item);
        }
        this.itemData = data;
        this.container.height = this.itemData.length * ITEM_HEIGHT;
        this.isSyncing = false;
        if(TOTAL_ITEMS < PAGE_ITEMS) this.isDataEnd = true;
        var offset = 0;
        if(rank > TOTAL_ITEMS - views || rank == 0) offset = TOTAL_ITEMS - views - 1;
        else if(rank > views) offset = rank - views - 1;
        this.container.y = this.node.height / 2 + offset * ITEM_HEIGHT;
    },
    
    update(dt) {
        if(this.isSyncing) return;

        var offsetY = this.scroller.getScrollOffset().y;
        var isDown = offsetY - this.lastOffset > 0;
        var currPos = Math.floor(offsetY / ITEM_HEIGHT);
        this.lastOffset = offsetY;

        if(isDown) {
            if(currPos + VIEW_ITEMS > this.itemData.length && !this.isDataEnd) {
                this.syncData();
                return;
            }
            for(var i in this.allItems) {
                var item = this.allItems[i];
                var nextOrder = item.order + VIEW_ITEMS;
                if(item.order < currPos && nextOrder < this.itemData.length) {
                    item.order = nextOrder;
                    item.node.y -= ITEM_HEIGHT * VIEW_ITEMS;
                    this.updateItem(item, this.itemData[nextOrder], nextOrder);
                }
            }
        } else {
            for(var i in this.allItems) {
                var item = this.allItems[i];
                var nextOrder = item.order - VIEW_ITEMS;
                if(item.order >= currPos + VIEW_ITEMS && nextOrder >= 0) {
                    item.order = nextOrder;
                    item.node.y += ITEM_HEIGHT * VIEW_ITEMS;
                    this.updateItem(item, this.itemData[nextOrder], nextOrder);
                }
            }
        }
    },

    syncData() {
        this.isSyncing = false;
        this.isDataEnd = true;
    },

    initItem(node, data, order) {
        var item = node.getComponent('AwardItem');
        node.cascadeOpacity = false;
        this.updateItem(item, data, order);
        return item;
    },

    updateItem(item, data, order) {
        item.node.find('bg').active = data.rank == this.nowRank;
        item.label1.string = data.rank;
        item.label2.string = data.nickName;
        item.label3.string = data.amount;
        item.label4.string = data.awardName;
        item.node.opacity = order % 2 == 0 ? 0 : 255;
    }
});
